#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
简化版运行脚本
"""

try:
    print("正在启动PoseGuard姿态评估系统...")
    print("=" * 50)
    
    # 导入模块
    print("导入模块...")
    import cv2
    import mediapipe as mp
    import numpy as np
    print("✅ 基础模块导入成功")
    
    # 导入项目模块
    from pose_detector import PoseDetector
    from pose_classifier import PoseClassifier
    from action_detector import ActionDetector
    from behavior_detector import BehaviorDetector
    print("✅ 项目模块导入成功")
    
    # 初始化检测器
    print("初始化检测器...")
    detector = PoseDetector()
    classifier = PoseClassifier()
    action_detector = ActionDetector()
    behavior_detector = BehaviorDetector()
    print("✅ 检测器初始化成功")
    
    # 测试摄像头
    print("测试摄像头...")
    cap = cv2.VideoCapture(0)
    if not cap.isOpened():
        print("❌ 无法打开摄像头")
        exit(1)
    
    print("✅ 摄像头打开成功")
    print("按 'q' 键退出程序")
    
    # 主循环
    while True:
        ret, frame = cap.read()
        if not ret:
            print("无法读取摄像头画面")
            break
        
        # 检测姿态
        landmarks, annotated_image = detector.detect_pose(frame)
        
        if landmarks is not None:
            # 分类姿态
            pose_result = classifier.classify_pose(landmarks)
            pose_name = pose_result.get('pose', 'unknown')
            pose_conf = pose_result.get('confidence', 0.0)
            
            # 在图像上显示信息
            cv2.putText(annotated_image, f"Pose: {pose_name}", (10, 30), 
                       cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
            cv2.putText(annotated_image, f"Conf: {pose_conf:.2f}", (10, 70), 
                       cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
            
            print(f"检测到姿态: {pose_name} (置信度: {pose_conf:.2f})")
        
        # 显示图像
        cv2.imshow('PoseGuard - 姿态评估系统', annotated_image)
        
        # 检查按键
        key = cv2.waitKey(1) & 0xFF
        if key == ord('q'):
            break
    
    cap.release()
    cv2.destroyAllWindows()
    print("程序已退出")

except Exception as e:
    print(f"程序运行出错: {e}")
    import traceback
    traceback.print_exc()
